package cn.tedu.jws.admin.controller;

import cn.tedu.jws.admin.pojo.dto.OwnerAddNewDTO;
import cn.tedu.jws.admin.pojo.dto.OwnerUpdateDTO;
import cn.tedu.jws.admin.pojo.vo.OwnerListVO;
import cn.tedu.jws.admin.pojo.vo.OwnerStandardVO;
import cn.tedu.jws.admin.service.OwnerService;
import cn.tedu.jws.admin.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/jws/admin/owner")
@Api(tags = "04.业主管理")
public class OwnerController {

    @Autowired
    private OwnerService ownerService;

    @PostMapping("/add-new")
    @ApiOperation("添加业主")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@Valid OwnerAddNewDTO ownerAddNewDTO){
        log.debug("开始处理【添加业主】的请求，参数：{}", ownerAddNewDTO);
        ownerService.addNew(ownerAddNewDTO);
        return JsonResult.ok();
    }

    @PostMapping("/{id}/delete")
    @ApiOperation("根据ID删除业主")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "业主ID", required = true, dataType = "long")
    })
    public JsonResult<Void> delete(@PathVariable @Range(min = 1, message = "请提交有效的ID值！")Long id){
        log.debug("开始处理【删除业主】的请求，ID：{}",id);
        ownerService.deleteById(id);
        return JsonResult.ok();
    }

    @GetMapping("/{id}")
    @ApiOperation("根据ID查询业主")
    @ApiOperationSupport(order = 400)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "业主ID", required = true, dataType = "long")
    })
    public JsonResult<OwnerStandardVO> selectById(@PathVariable @Range(min = 1, message = "请提交有效的ID值！") Long id){
        log.debug("开始处理根据ID查询业主信息:id为{}",id);
        OwnerStandardVO standardById = ownerService.getStandardById(id);
        return JsonResult.ok(standardById);
    }

    @GetMapping("")
    @ApiOperation("查询业主列表")
    @ApiOperationSupport(order = 410)
    public JsonResult<List<OwnerListVO>> select(){
        List<OwnerListVO> list = ownerService.list();
        return JsonResult.ok(list);
    }

    @PostMapping("/{id}/update")
    @ApiOperation("修改业主信息详情")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "相册ID", required = true, dataType = "long")
    })
    public JsonResult<Void> updateInfoById(@PathVariable @Range(min = 1, message = "请提交有效的ID值！") Long id,
                                           @Valid OwnerUpdateDTO ownerUpdateDTO) {
        log.debug("开始处理【修改业主详情】的请求，ID：{}，新数据：{}", id, ownerUpdateDTO);
        ownerService.updateInfoById(id, ownerUpdateDTO);
        return JsonResult.ok();
    }
}
